package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.bo.UserBo;
import com.ruoyi.system.domain.vo.InnerObjVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.User;
import com.ruoyi.business.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 普通用户Controller
 * 
 * @author 吴倩芸
 * @date 2023-03-04
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/business/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询普通用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出普通用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:export')")
    @Log(title = "普通用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "普通用户数据");
    }

    /**
     * 获取普通用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增普通用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:add')")
    @Log(title = "普通用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserBo bo)
    {
        return toAjax(userService.insertUser(bo));
    }

    /**
     * 修改普通用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:edit')")
    @Log(title = "普通用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除普通用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:remove')")
    @Log(title = "普通用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }


    @Log(title = "根据用户的名字来查询用户的内部对象", businessType = BusinessType.OTHER)
    @GetMapping("/ref")
    public AjaxResult queryRefUserInnerObj(@RequestParam(value = "name", defaultValue = "") String name) {
        List<InnerObjVo> list = userService.queryRefUserInnerObj(name);
        return AjaxResult.success(list);
    }
}
